#ifndef _DEV_LCD_
#define _DEV_LCD_

#include "data_type.h"

/* MCULCD 屏幕相关 */
#define DEV_LCD_ILI9341_ID          0x9341
#define DEV_LCD_NT35310_ID          0x5310
#define DEV_LCD_NT35510_ID          0x8000
#define DEV_LCD_SSD1963_ID          0x5761

//LCD分辨率设置
#define DEV_LCD_SSD_HOR_RESOLUTION        800        //LCD水平分辨率
#define DEV_LCD_SSD_VER_RESOLUTION        480        //LCD垂直分辨率
//LCD驱动参数设置
#define DEV_LCD_SSD_HOR_PULSE_WIDTH       1        //水平脉宽
#define DEV_LCD_SSD_HOR_BACK_PORCH        46        //水平前廊
#define DEV_LCD_SSD_HOR_FRONT_PORCH       210        //水平后廊

#define DEV_LCD_SSD_VER_PULSE_WIDTH       1        //垂直脉宽
#define DEV_LCD_SSD_VER_BACK_PORCH        23        //垂直前廊
#define DEV_LCD_SSD_VER_FRONT_PORCH       22        //垂直前廊
//如下几个参数，自动计算
#define DEV_LCD_SSD_HT     (DEV_LCD_SSD_HOR_RESOLUTION + DEV_LCD_SSD_HOR_BACK_PORCH + DEV_LCD_SSD_HOR_FRONT_PORCH)
#define DEV_LCD_SSD_HPS    (DEV_LCD_SSD_HOR_BACK_PORCH)
#define DEV_LCD_SSD_VT     (DEV_LCD_SSD_VER_RESOLUTION + DEV_LCD_SSD_VER_BACK_PORCH + DEV_LCD_SSD_VER_FRONT_PORCH)
#define DEV_LCD_SSD_VPS    (DEV_LCD_SSD_VER_BACK_PORCH)

/* 扫描方向定义 */
#define DEV_LCD_DIR_L2R_U2D  0         /* 从左到右,从上到下 */
#define DEV_LCD_DIR_L2R_D2U  1         /* 从左到右,从下到上 */
#define DEV_LCD_DIR_R2L_U2D  2         /* 从右到左,从上到下 */
#define DEV_LCD_DIR_R2L_D2U  3         /* 从右到左,从下到上 */
#define DEV_LCD_DIR_U2D_L2R  4         /* 从上到下,从左到右 */
#define DEV_LCD_DIR_U2D_R2L  5         /* 从上到下,从右到左 */
#define DEV_LCD_DIR_D2U_L2R  6         /* 从下到上,从左到右 */
#define DEV_LCD_DIR_D2U_R2L  7         /* 从下到上,从右到左 */

#define DEV_LCD_DIR_DEFAULT  DEV_LCD_DIR_L2R_U2D    /* 设置默认扫描方向 */

/* RGBLCD 屏幕相关 */

/* 屏幕型号 */
#define DEV_RGBLCD_4_3_R480X272_ID      0x4342     /* 4.3寸屏, 480*272分辨率 */
#define DEV_RGBLCD_7_R800X480_ID        0x7084     /* 7寸屏, 800*480分辨率 */
#define DEV_RGBLCD_7_R1024X600_ID       0X7016     /* 7寸屏, 1024*600分辨率 */
#define DEV_RGBLCD_10_1_R1280X800_ID    0X1018     /* 10.1寸屏, 1280*800分辨率 */

/* 公共宏 */
/* 控制方向 */
#define DEV_LCD_CTRL_VER             0      /* 控制方向: 竖直 */
#define DEV_LCD_CTRL_HOR             1      /* 控制方向: 水平 */

#define DEV_LCD_CTRL_DIR_DEFAULT     DEV_LCD_CTRL_HOR  /* 设置默认控制方向 */

/* 像素格式配置 */
#define DEV_RGBLCD_PIXEL_FORMAT_ARGB8888    0X00
#define DEV_RGBLCD_PIXEL_FORMAT_RGB888      0X01
#define DEV_RGBLCD_PIXEL_FORMAT_RGB565      0X02
#define DEV_RGBLCD_PIXEL_FORMAT_ARGB1555    0X03
#define DEV_RGBLCD_PIXEL_FORMAT_ARGB4444    0X04
#define DEV_RGBLCD_PIXEL_FORMAT_L8          0X05
#define DEV_RGBLCD_PIXEL_FORMAT_AL44        0X06
#define DEV_RGBLCD_PIXEL_FORMAT_AL88        0X07

#define DEV_RGBLCD_PIXEL_FORMAT_CFG         DEV_RGBLCD_PIXEL_FORMAT_RGB565

//画笔颜色
#define LCD_COLOR_WHITE              0xFFFFFF
#define LCD_COLOR_BLACK              0x0000
#define LCD_COLOR_BLUE               0x001F
#define LCD_COLOR_BRED               0XF81F
#define LCD_COLOR_GRED               0XFFE0
#define LCD_COLOR_GBLUE              0X07FF
#define LCD_COLOR_RED                0xF800
#define LCD_COLOR_MAGENTA            0xF81F
#define LCD_COLOR_GREEN              0x07E0
#define LCD_COLOR_CYAN               0x7FFF
#define LCD_COLOR_YELLOW             0xFFE0
#define LCD_COLOR_BROWN              0XBC40 //棕色
#define LCD_COLOR_BRRED              0XFC07 //棕红色
#define LCD_COLOR_GRAY               0X8430 //灰色
//GUI颜色

#define LCD_COLOR_DARKBLUE           0X01CF    //深蓝色
#define LCD_COLOR_LIGHTBLUE          0X7D7C    //浅蓝色  
#define LCD_COLOR_GRAYBLUE           0X5458 //灰蓝色
//以上三色为PANEL的颜色 
 
#define LCD_COLOR_LIGHTGREEN         0X841F //浅绿色
//#define LCD_COLOR_LIGHTGRAY        0XEF5B //浅灰色(PANNEL)
#define LCD_COLOR_LGRAY              0XC618 //浅灰色(PANNEL),窗体背景色

#define LCD_COLOR_LGRAYBLUE          0XA651 //浅灰蓝色(中间层颜色)
#define LCD_COLOR_LBBLUE             0X2B12 //浅棕蓝色(选择条目的反色)

/* LCD背光状态 */
enum
{
    DEV_LCD_BACKLIGHT_OFF = 0, /* 背光关 */
    DEV_LCD_BACKLIGHT_ON,      /* 背光开 */
};

enum
{
    DEV_LCD_DISPLAY_ON = 0,     /* 显示开 */
    DEV_LCD_DISPLAY_OFF,        /* 显示关 */
};

/* LCD类型 */
enum
{
    DEV_LCD_TYPE_RGB = 0,   /* RGB屏 */
    DEV_LCD_TYPE_MCU,       /* MCU屏 */
};

/* MCU屏结构体 */
//LCD地址结构体
typedef struct
{
    volatile uint16_t Reg;
    volatile uint16_t Ram;
} LCD_TypeDef;

/* 使用NOR/SRAM的 Bank1.sector1,地址位HADDR[27,26]=00 A18作为数据命令区分线 */
/* 注意设置时STM32内部会右移一位对其! */
#define LCD_BASE        ((uint32_t ) (0x60000000 | 0x0007FFFE))
#define LCD             ((LCD_TypeDef *) LCD_BASE)

typedef struct DEV_MCULCD
{
    uint16_t width;        /* 屏幕宽度像素 */
    uint16_t height;       /* 屏幕高度像素 */
    uint16_t id;           /* LCD ID */
    uint8_t  ctrl_dir;     /* 横屏还是竖屏控制: 0, 竖屏; 1, 横屏 */
    uint16_t wr_gram_cmd;  /* 开始写gram指令 */
    uint16_t set_xpos_cmd; /* 设置x坐标指令 */
    uint16_t set_ypos_cmd; /* 设置y坐标指令 */
} dev_mculcd;
typedef dev_mculcd *dev_mculcd_t;

/* RGB屏结构体 */
typedef struct DEV_RGBLCD
{
    uint32_t pwidth;         //LCD面板的宽度,固定参数,不随显示方向改变,如果为0,说明没有任何RGB屏接入
    uint32_t pheight;        //LCD面板的高度,固定参数,不随显示方向改变
    uint16_t hsw;            //水平同步宽度
    uint16_t vsw;            //垂直同步宽度
    uint16_t hbp;            //水平后廊
    uint16_t vbp;            //垂直后廊
    uint16_t hfp;            //水平前廊
    uint16_t vfp;            //垂直前廊
    uint16_t width;          //LCD宽度
    uint16_t height;         //LCD高度
    uint32_t pixsize;        //每个像素所占字节数
    uint8_t  active_layer;    //当前层编号:0/1
    uint8_t  ctrl_dir;       /* 横屏还是竖屏控制: 0, 竖屏; 1, 横屏 */
    uint16_t id;             /* LCD ID */
} dev_rgblcd;
typedef dev_rgblcd *dev_rgblcd_t;

typedef struct DEV_LCD
{
    dev_mculcd *mculcd;    /* mculcd */
    dev_rgblcd *rgblcd;    /* rgblcd */
    uint8_t    type;       /* 屏幕类型 */
} dev_lcd;
typedef dev_lcd *dev_lcd_t;

RET_STATUS dev_lcd_init(void);
void dev_lcd_show_info(void);
void dev_lcd_backlight_ctrl(uint8_t status);
void dev_lcd_display_switch(uint8_t status);
void dev_lcd_set_cursor(uint16_t xpos, uint16_t ypos);
void dev_lcd_fill_color(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint32_t color);
void dev_lcd_fill_colors(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint16_t *colors);
void dev_lcd_clear(void);
void dev_lcd_draw_point(uint16_t xpos, uint16_t ypos, uint32_t color);

#endif
